<template>
	<view class="">
		<view class="guanli" @click="administration">
			管理
		</view>
		<u-empty text="购车暂无商品" mode="car" v-if="list.length==0"></u-empty>
		<view class="box" v-for="(item, index) in list" :key="index">
			<u-checkbox-group :wrap="true" shape="circle" active-color="#ff1212">
				<u-checkbox @change="checkboxChange(index)" v-model="item.checked" :name="item.getcommoditynum.name">
					<view class="cont">
						<image :src="item.getcommoditynum.img?item.getcommoditynum.img:item.commodity_id.img" mode="">
						</image>
						<view class="rightbox">
							<view class="name">
								{{item.getcommoditynum.name}}
							</view>
							<view class="jiesao">
								{{item.commodity_attribute}}
							</view>
							<view class="jiage">
								<view class="jtjg">
									<text class="danwei">￥</text>
									<text class="price">{{item.getcommoditynum.prices}}</text>
								</view>
								<u-number-box v-model="item.num" @change="valChange" min="1"></u-number-box>
							</view>
						</view>
					</view>
				</u-checkbox>
			</u-checkbox-group>
		</view>
		<van-submit-bar :price="flag?total:0" :button-text="flag?'提交订单':'删除'" @submit="onClickButton">
			<u-checkbox @change="allcheckboxChange" v-model="statisticsIndex" shape="circle" active-color="#ff1212"
				v-if="statisticsIndex">全选
			</u-checkbox>
			<u-checkbox @change="allcheckboxChange" v-model="statisticsIndex" shape="circle" active-color="#ff1212"
				v-else>全选
			</u-checkbox>
		</van-submit-bar>
	</view>
</template>

<script>
	import {
		getshopingcartlist,
		submitorder,
		deleteshopingcart
	} from "../../api/api/index.js"
	export default {
		data() {
			return {
				list: [],
				statisticsIndex: false,
				flag: true,
				value: 1,
				flags: false,
				page: 0,
				limit: 20,
				identity: 0,
				xuanzhonglist: [],
				total: 0
			}
		},
		methods: {
			// 全选
			allcheckboxChange() {
				if (this.statisticsIndex) {
					this.list.find((item, index) => {
						item.checked = false
					})
					this.statisticsIndex = false
				} else {
					this.list.find((item, index) => {
						item.checked = true
					})
					this.statisticsIndex = true
				}
				this.statistics()
			},
			hanshu() {
				let a = true
				this.list.forEach(function(item, index) {
					if (!(item.checked == true)) {
						a = false
					}
				})
				if (a) {
					this.statisticsIndex = true
				} else {
					this.statisticsIndex = false
				}
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(v) {
				this.list[v].checked = !this.list[v].checked
				this.statistics()
				this.hanshu()
			},
			valChange() {
			},
			statistics() {
				let total = 0
				this.list.forEach((item, index) => {
					if (item.checked == true) {
						total = total + item.getcommoditynum.price * item.num
					}
				})
				this.total = total*100
			},
			// 提交订单
			onClickButton(e) {
				if(this.flag){
					let data = []
					let x = this.list.filter(v => {
						return v.checked == true
					})
					// console.log(x)
					x.forEach((item, index) => {
						data[index] = {
							commodity_id: item.commodity_id.commodity_id,
							num_id: item.num_id,
							num: item.num
						}
					})
					let a = JSON.stringify(data)
					this.jumpLink({
						link: "/pages/Confirm_purchase/index",
						query: {
							list: a,
							id: 0
						}
					})
				}else{
					let ids = []
					let x = this.list.filter(v => {
						return v.checked == true
					})
					x.forEach((item, index) => {
						ids.push(item.id)
						// return item.id
					})
					// console.log(ids)
					uni.showLoading({
						title:"加载中",
						mask:true
					})
					deleteshopingcart({ids:ids}).then(res=>{
						uni.hideLoading()
						this.flags = false
						this.page = 0
						this.list = []
						uni.showToast({
							title:res.msg,
							icon:"none",
							duration:1500
						})
					})
				}
			},
			// 管理
			administration() {
				this.flag = !this.flag
			},
			// 接口
			public(flags) {
				if (flags) {
					this.flags = false
					this.page = 0
					this.list = []
				}
				this.page++
				let data = {
					page: this.page,
					limit: this.limit
				}
				getshopingcartlist(data).then(res => {
					if (res.data.length < this.limit) {
						this.flag = true
					}
					let xhlist = res.data.map(v => {
						if (this.identity == 0) {
							v.getcommoditynum.prices = v.getcommoditynum.price
						} else {
							v.getcommoditynum.prices = v.getcommoditynum.distributors_price
						}
						v.checked = false
						return v
					})
					this.list = this.list.concat(xhlist)
				})
			}
		},
		// computed: {
		// 	checked() {
		// 		var flge = true
		// 		this.list.forEach(v => {
		// 			if (!v.checked) {
		// 				flge = false
		// 			}
		// 		})
		// 		return flge
		// 	}
		// },
		onLoad() {
			this.identity = uni.getStorageSync("identity")
		},
		onShow() {
			this.public()
		}
	}
</script>

<style scoped lang="scss">
	.guanli {
		font-family: MicrosoftYaHei;
		font-size: 24rpx;
		color: #000000;
		margin-left: 670rpx;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.box {
		width: 704rpx;
		height: 200rpx;
		background-color: #ffffff;
		padding: 30rpx 10rpx;
		margin-bottom: 10rpx;
		margin-left: 20rpx;

		.cont {
			display: flex;
			align-items: center;

			image {
				width: 120rpx;
				height: 120rpx;
			}

			.rightbox {
				margin-left: 20rpx;
				width: 488rpx;

				.name {
					font-family: MicrosoftYaHei;
					font-size: 24rpx;
					color: #282828;
				}

				.jiesao {
					font-family: MicrosoftYaHei;
					font-size: 20rpx;
					color: #7c7c7c;
					// margin-top: 10rpx;
				}

				.jiage {
					display: flex;
					// margin-top: 10rpx;
					justify-content: space-between;

					.danwei {
						font-family: PingFang-SC-Bold;
						font-size: 22rpx;
						color: #ff1f1f;
					}

					.price {
						font-family: PingFang-SC-Bold;
						font-size: 28rpx;
						color: #ff1f1f;
					}
				}
			}
		}
	}
</style>
